<template>
  <view class="uni-container company-other-license">

    <uni-popup ref="message" type="message">
				<uni-popup-message type="error" :message="messageText" :duration="1500"></uni-popup-message>
		</uni-popup>

    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="company_license_icon" label="经营许可证" required>
        <uni-file-picker ref="files" @select="(e)=>fileList=e" @delete="()=>fileList==null" :auto-upload="false" file-mediatype="image" file-extname="jpg,png" :limit="1" return-type="array" v-model="formData.company_license_icon"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="company_license_type" label="许可证件类型" required>
        <uni-data-checkbox mode="button" v-model="formData.company_license_type" :localdata="formOptions.company_license_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="company_approval_date" label="核准日期" required>
        <uni-datetime-picker type="date" return-type="timestamp" v-model="formData.company_approval_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="company_license_valid_date" label="有效期至" required>
        <uni-datetime-picker type="date" return-type="timestamp" v-model="formData.company_license_valid_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="company_license_code" label="许可证编号" required>
        <uni-easyinput type="text" placeholder="请填写许可证上的许可证编号" v-model="formData.company_license_code" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_name" label="经营者名称" required>
        <uni-easyinput type="text" placeholder="请填写许可证上的经营者名称" v-model="formData.company_license_name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_credit_code" label="统一社会信用代码" required>
        <uni-easyinput type="text" placeholder="请填写许可证上的统一社会信用代码" v-model="formData.company_license_credit_code" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_holder_name" label="法定代表人/负责人" required>
        <uni-easyinput type="text" placeholder="请填写许可证上的法定代表人/负责人" v-model="formData.company_license_holder_name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_residence" label="住所" required>
        <uni-easyinput type="textarea" :maxlength="500" placeholder="请填写许可证上的住所" v-model="formData.company_license_residence" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_work_location" label="经营场所" required>
        <uni-easyinput type="textarea" :maxlength="500" placeholder="请填写许可证上的经营场所" v-model="formData.company_license_work_location" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_main_business" label="主体业态" required>
        <uni-easyinput type="textarea" :maxlength="500" placeholder="请填写许可证上的主体业态" v-model="formData.company_license_main_business" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="company_license_business_scope" label="经营项目/范围" required>
        <uni-easyinput type="textarea" :maxlength="500" placeholder="请填写许可证上的经营项目/范围" v-model="formData.company_license_business_scope" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <lay-button type="normal" @click="submit">确认提交</lay-button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <lay-button @click="closeAll">取消关闭</lay-button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/company-other-license.js';
  import {store} from '@/uni_modules/uni-id-pages/common/store.js'

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'company-other-license';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "merchant_id": store.userInfo.merchant_id,
        "company_license_icon": [],
        "company_license_type": 0,
        "company_approval_date": null,
        "company_license_valid_date": null,
        "company_license_code": "",
        "company_license_name": "",
        "company_license_credit_code": "",
        "company_license_holder_name": "",
        "company_license_residence": "",
        "company_license_work_location": "",
        "company_license_main_business": "",
        "company_license_business_scope": ""
      }
      return {
        fileList:null,
        messageText:``,
        
        formData,
        formOptions: {
          "company_license_type_localdata": [
            {
              "text": "食品经营许可证",
              "value": 0
            },
            {
              "text": "旅行社业务经营许可证",
              "disable": true,
              "value": 1
            },
            {
              "text": "人力资源许可证",
              "disable": true,
              "value": 2
            },
            {
              "text": "人力资源许可证",
              "disable": true,
              "value": 3
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then(async (res) => {

          if(this.fileList==null){
            this.messageText=`经营许可证必须上传`;
            return this.$refs.message.open()
          }

          res.company_license_icon= await this.$refs.files.upload();
          res.merchant_id= store.userInfo.merchant_id

          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
